package com.example.lijuan.myapplication.Adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.TextView;

import com.example.lijuan.myapplication.R;
import com.example.lijuan.myapplication.module.Trip;

import java.util.ArrayList;
import java.util.List;

/**
 * Class com.sita.bike.ui.adapter is for
 *
 * @author mark on 15/5/26.
 */
public class TripHistoryListAdapter extends RecyclerView.Adapter<TripHistoryListAdapter.TripListViewHolder> {

    private List<Trip> mTripList;
    private Context context;
    private OnItemButtonClickListener mListener;

    public TripHistoryListAdapter(Context context, List<Trip> trips) {
        this.context = context;
        this.mTripList = trips == null ? new ArrayList<Trip>() : trips;
    }

    @Override
    public TripListViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
        View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.fragment_trip_item, viewGroup, false);
        TripHistoryListAdapter.TripListViewHolder mh = new TripListViewHolder(v);
        return mh;
    }

    public void setOnItemButtonClickListener(OnItemButtonClickListener listener) {
        mListener = listener;
    }

    public Trip getItem(int position) {
        return mTripList.size() == 0 ? null : mTripList.get(position);
    }

    @Override
    public void onBindViewHolder(final TripListViewHolder tripListViewHolder, final int i) {
        Trip trip = mTripList.get(i);

        tripListViewHolder.tripView.setText(trip.getFromTime() + "~" + trip.getArriveTime());
        tripListViewHolder.tripView.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mListener != null) {
                    mListener.onItemButtonClick(v, i);
                }
            }
        });

    }

    @Override
    public int getItemCount() {
        return (null != mTripList ? mTripList.size() : 0);
    }

    public interface OnItemButtonClickListener {
        public void onItemButtonClick(View item, int position);
    }

    public static class TripListViewHolder extends RecyclerView.ViewHolder {
        TextView tripView;

        public TripListViewHolder(View itemView) {
            super(itemView);
            tripView = (TextView) itemView.findViewById(R.id.trip_item_content);
        }

    }
}
